package com.ivan.toolkit.swing;

import java.awt.BorderLayout;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;

public class Splashor {
	
	private static final int ProgressBarHeight = 6;

	private Icon icon 		= null;
	
	private JWindow window				= null;
	//private JProgressBar progressBar 	= null;
	private JLabel label 				= null;
	//private Timer timer;
	
	//private int percent = 0;
	
	//private int speed = 10;
	
	public static Splashor create(String file) {
		Icon icon  = new ImageIcon(Splashor.class.getResource(file));
		return create(icon);
	}
	
	public static Splashor create(Icon icon) {
		
		if(icon == null) {
			return null;
		}
		
		if(icon.getIconWidth() == 0 || icon.getIconHeight() == 0) {
			return null;
		}
		
		return new Splashor(icon);
		
	}

	private Splashor(Icon icon) {
		this.icon = icon;
	}

	void createWindow() {
		
		if(window != null) {
			return;
		}
		
		window = new JWindow();
		window.setSize(icon.getIconWidth(), icon.getIconHeight() + ProgressBarHeight);
		window.setLocationRelativeTo(null);
		
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout(0, 0));
		window.setContentPane(panel);
		
		{
			this.label = new JLabel("");
			this.label.setIcon(icon);
			panel.add(this.label, BorderLayout.CENTER);
		}
		
		window.setVisible(true);
	}

	public void start() {
		createWindow();
	}

	public void stop() {
		
		if(window != null) {
			window.setVisible(false);
			window.dispose();
			window = null;
		}
		
	}
	
}
